<!DOCTYPE html>
<!--[if IE 8]>
<html class="ie ie8"> <![endif]-->
<!--[if IE 9]>
<html class="ie ie9"> <![endif]-->
<!--[if gt IE 9]><!-->
<html> <!--<![endif]-->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="static code scan">
    <meta name="description" content="Cobra is a code static security scan system">
    <meta name="author" content="Feei <feei@feei.cn>">
    <title>Cobra</title>

    <!-- Favicon-->
    <link rel="shortcut icon" href="{{ url_for('static', filename='ico/favicon.ico') }}" type="image/x-icon">

    <!-- CSS -->
    <link href="{{ url_for('static', filename='css/base.css') }}" rel="stylesheet">

    <!-- SPECIFIC CSS -->
    <link href="asset/css/skins/square/grey.css" rel="stylesheet">

    <style>
        .bar {
            width: 0;
            height: 30px;
            background: #18cc18;
            line-height: 30px;
            color: #fff;
            font-weight: bold;
        }
    </style>

    <!--[if lt IE 9]>
    <script src="asset/js/html5shiv.min.js"></script>
    <script src="asset/js/respond.min.js"></script>
    <![endif]-->

</head>
<body>

<!--[if lte IE 8]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade
    your browser</a>.</p>
<![endif]-->

<div id="preloader">
    <div class="sk-spinner sk-spinner-wave">
        <div class="sk-rect1"></div>
        <div class="sk-rect2"></div>
        <div class="sk-rect3"></div>
        <div class="sk-rect4"></div>
        <div class="sk-rect5"></div>
    </div>
</div>
<!-- End Preload -->

<div class="layer"></div>
<!-- Mobile menu overlay mask -->

{% include "header.html" %}

<section id="search_container">
    <div id="search">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#config" data-toggle="tab">VCS</a></li>
            <li><a href="#log" data-toggle="tab">UPLOAD</a></li>
        </ul>

        <div class="tab-content">
            <div class="tab-pane active" id="config">
                <h3>Github / Gitlab</h3>
                <div class="row">
                    <div class="col-md-12">
                        <div class="col-md-12" style="padding-left:0;padding-right:0;">
                            <div class="form-group">
                                <label>GIT repository address (.git at the end) or local project path</label>
                                <input type="text" class="form-control" name="repository"
                                       placeholder="Ex:http://github.com/FeeiCN/grw.git or /home/Feei/project_name">
                            </div>
                        </div>
                        <div class="col-md-12" style="padding-left:0;padding-right:0;">
                            <div class="form-group">
                                <label>Branch / Tag</label>
                                <input type="text" class="form-control" name="branch"
                                       placeholder="Default: master, optional" value="master">
                            </div>
                        </div>
                    </div>
                </div><!-- End row -->
                <hr>
                <button class="btn_1 green" id="scan"><i class="icon-search"></i>Scan</button>
            </div><!-- End rab -->
            <div class="tab-pane" id="log">
                <h3>Select a compressed file scan</h3>
                <div class="col-md-12">
                    <div class="box_style_2" id="dropbox">
                        <i class="icon-upload-cloud"></i>
                        <h4><span>Drag and drop</span> upload</h4>
                        <div align="center" id="selectFile"></div>
                        <hr>
                        <input type="file" class="btn_1" id="uploadFile" name="file" style="width: 100%;" accept="application/bacnet-xdd+zip,.tar,.gz,.rar,.zip,.tgz,.bz2">
                        <br>
                        <small>Support for upload extensions: tar.bz2|tar|gz|tgz|tar.gz|rar|zip</small>
                        <small>只允许单文件上传，不允许文件夹拖拽</small>

                        <div id="progress">
                            <div class="bar"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

{% include "index-footer.html" %}

<!-- Common scripts -->
<script src="asset/js/jquery-1.11.2.min.js"></script>
<script src="asset/js/common_scripts_min.js"></script>
<script src="asset/js/functions.js"></script>
<script src="{{ url_for('static', filename='js/jquery.plainoverlay.min.js') }}"></script>
<!-- Upload -->
<script src="{{ url_for('static', filename='js/jquery.ui.widget.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.iframe-transport.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.fileupload.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.fileupload-process.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.fileupload-validate.js') }}"></script>
<script>

    var fileList;
    var dropbox = document.getElementById('dropbox');
    dropbox.addEventListener("drop", function (e) {
        e.preventDefault();
        fileList = e.dataTransfer.files;
    });

    // click and select file to upload
    var files;
    $('input[type=file]').on('change', prepareUpload);

    function prepareUpload(event) {
        files = event.target.files;
    }

    $(function () {
        // Tab1
        $("#scan").on('click', function (e) {
            e.preventDefault();
            $('.tab-content').plainOverlay('show');
            var url = $("input[name=repository]").val();
            var branch = $("input[name=branch]").val();
            if (url.substring(0, 4) === 'http') {
                var target = url + ':' + branch;
            } else {
                target = url;
            }

            var old_version = $("input[name=old_version]").val();
            var new_version = $("input[name=new_version]").val();

            var data = {
                'target': target,
                'key': "{{ key }}"
            };
            $.ajax({
                url: '/api/add',
                type: 'POST',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                async: true,
                success: function (result) {
                    if (result.code === 1001) {
                        var sid = result.result.sid;
                        var data = {sid: sid, key: "{{ key }}"};

                        function get_status() {
                            $.ajax({
                                url: '/api/status',
                                type: 'POST',
                                data: JSON.stringify(data),
                                contentType: 'application/json; charset=utf-8',
                                dataType: 'json',
                                async: false,
                                success: function (result) {
                                    if (result.code === 1001) {
                                        if (result.result['status'] === 'done') {
                                            $('.tab-content').plainOverlay('hide');
                                            window.location = result.result['report'];
                                        } else {
                                            setTimeout(get_status, 3000);
                                        }
                                    } else {
                                        alert(result.msg);
                                    }
                                }
                            });
                        }

                        get_status();
                    } else {
                        $('.tab-content').plainOverlay('hide');
                        alert(result.msg);
                    }
                }
            });
        });
        // Tab2
        var process_bar = $('#progress .bar');
        $('#uploadFile').fileupload({
            url: '/api/upload',
            dataType: 'json',
            acceptFileTypes: /(\.|\/)(bz2|tar|gz|tgz|rar|zip)$/i,
            maxNumberOfFiles: 1,
            processfail: function (e, result) {
                $('#progress').empty();
                $('#progress').html('文件类型错误！');
            },
            done: function (e, result) {
                if (result.result.code === 1001) {
                    var sid = result.result.result.sid;
                    var data = {sid: sid, key: "{{ key }}"};

                    function get_status() {
                        $.ajax({
                            url: '/api/status',
                            type: 'POST',
                            data: JSON.stringify(data),
                            contentType: 'application/json; charset=utf-8',
                            dataType: 'json',
                            async: false,
                            success: function (result) {
                                if (result.code === 1001) {
                                    if (result.result.status === 'done') {
                                        $('.tab-content').plainOverlay('hide');
                                        window.location = result.result['report'];
                                        process_bar.css('width', '0%');
                                    } else {
                                        setTimeout(get_status, 3000);
                                    }
                                } else {
                                    alert(result.result);
                                }
                            }
                        });
                    }

                    get_status();
                } else {
                    alert(result.result.result);
                    location.reload();
                }
            },
            start: function (e) {
                $('.tab-content').plainOverlay('show');
                process_bar.css('width', '0%');
            },
            stop: function (e) {
                process_bar.text('scanning...');
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                process_bar.css(
                    'width',
                    progress + '%'
                );
                process_bar.text('uploading(' + progress + '%)');
            }
        });
    });

</script>
</body>
</html>
